package models.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import models.Disciplina;
import play.db.DB;
/**
 *
 * @author User
 */
public class DAODisciplina implements DAO<Disciplina>
{
    public void inserir(Disciplina t)
    {
        try
        {
            Statement st = DB.getConnection().createStatement();
            String query = String.format("insert into Disciplina values(%d,\'%s\'); ",t.id, t.nome);
            st.execute(query);
           
        } catch (SQLException ex)
        {
            System.out.println("Erro: " + ex.getMessage());
        }
    }

    public void atualizar(Disciplina t)
    {
        try
        {
            Statement st = DB.getConnection().createStatement();
            String query = String.format("update Disciplina set nome = \'%s\' where id = %d", t.nome, t.id);
            st.execute(query);
        } catch (SQLException ex)
        {
            System.out.println("Erro: " + ex.getMessage());
        }
    }

    public void deletar(Disciplina t)
    {
        try
        {
            Statement st = DB.getConnection().createStatement();
            String query = String.format("delete from Disciplina where id = %d",t.id);
            st.execute(query);
        } catch (SQLException ex)
        {
            System.out.println("Erro: " + ex.getMessage());
        }
    }

    public List<Disciplina> listar()
    {
        List<Disciplina> retorno = new ArrayList<Disciplina>();
        try
        {
            Statement st = DB.getConnection().createStatement();
            String query = "select * from Disciplina";
            ResultSet rSet = st.executeQuery(query);

            while(rSet.next())
            {
                Disciplina disc = new Disciplina(rSet.getInt(1), rSet.getString(2));
                retorno.add(disc);
            }

        } catch (SQLException ex)
        {
            System.out.println("Erro: " + ex.getMessage());
        }

        return retorno;
    }

}
